package com.yy.launchercaijing

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.*
import android.webkit.WebChromeClient
import android.webkit.WebView
import android.webkit.WebViewClient

class WebviewActivity : AppCompatActivity() {

    private lateinit var webView: WebView

    @SuppressLint("NewApi")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_webview)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }

        enableFullScreen()

        var url = intent.getStringExtra("url")

        webView = findViewById<WebView>(R.id.webView)

        // 配置 WebView 设置
        webView.settings.apply {
            javaScriptEnabled = true // 启用 JavaScript
            domStorageEnabled = true // 启用 DOM Storage
            mediaPlaybackRequiresUserGesture = false // 自动播放视频
        }

        // 设置自定义 WebChromeClient 支持视频全屏
        webView.webChromeClient = CustomWebChromeClient()

        // 设置 WebViewClient，确保页面在 WebView 内加载
        webView.webViewClient = WebViewClient()

        // 加载网页
        url?.let { webView.loadUrl(it) } // 替换为需要加载的视频网址
    }

    override fun onBackPressed() {
        // 如果可以返回上一页面，则返回
        if (webView.canGoBack()) {
            webView.goBack()
        } else {
            super.onBackPressed()
        }
    }

    override fun onDestroy() {
        // 销毁 WebView 避免内存泄漏
        webView.destroy()
        super.onDestroy()
    }

    inner class CustomWebChromeClient : WebChromeClient() {

        private var customView: View? = null
        private var customViewCallback: CustomViewCallback? = null

        override fun onShowCustomView(view: View?, callback: CustomViewCallback?) {
            // 进入全屏模式
            if (customView != null) {
                callback?.onCustomViewHidden()
                return
            }

            customView = view
            customViewCallback = callback
            webView.visibility = View.GONE

            (window.decorView as ViewGroup).apply {
                addView(customView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
                systemUiVisibility = View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_IMMERSIVE
            }
        }

        override fun onHideCustomView() {
            // 退出全屏模式
            customView?.let {
                (window.decorView as ViewGroup).apply {
                    removeView(it)
                    systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
                }
                webView.visibility = View.VISIBLE
                customView = null
                customViewCallback?.onCustomViewHidden()
            }
        }

        override fun getDefaultVideoPoster(): Bitmap? {
            return super.getDefaultVideoPoster() ?: Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
        }
    }


    private fun enableFullScreen() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            // Android 11 及以上版本
            window.insetsController?.let { controller ->
                controller.hide(WindowInsets.Type.statusBars() or WindowInsets.Type.navigationBars())
                controller.systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
            }
        } else {
            // Android 11 以下版本
            window.decorView.systemUiVisibility = (
                    View.SYSTEM_UI_FLAG_FULLSCREEN
                            or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    )
        }
    }
}